<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SVG预览工具</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        /* SVG预览页面特定样式 */
        .preview-container {
            display: flex;
            flex-direction: column;
            max-width: 1200px;
            margin: 0 auto;
            height: calc(100vh - 80px);
        }
        
        .preview-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .back-btn {
            background: none;
            border: 1px solid #ddd;
            padding: 8px 16px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 16px;
            color: #333;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        
        .back-btn:hover {
            background-color: #f0f0f0;
        }
        
        .preview-main {
            display: flex;
            flex: 1;
            gap: 20px;
            overflow: hidden;
        }
        
        .code-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            min-width: 300px;
        }
        
        .preview-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            min-width: 300px;
        }
        
        .section-title {
            font-size: 18px;
            margin-bottom: 10px;
            font-weight: bold;
        }
        
        #svg-code {
            flex: 1;
            width: 100%;
            padding: 15px;
            font-family: 'Courier New', Courier, monospace;
            font-size: 14px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: none;
            overflow: auto;
            line-height: 1.5;
        }
        
        .preview-area {
            flex: 1;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 20px;
            overflow: auto;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }
        
        /* 响应式布局 */
        @media (max-width: 768px) {
            .preview-main {
                flex-direction: column;
            }
            
            .code-section, .preview-section {
                min-height: 300px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>SVG预览工具</h1>
    </div>
    
    <div class="preview-container">
        <div class="preview-header">
            <button class="back-btn" onclick="window.location.href='index.html'">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#333" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon">
                    <path d="M19 12H5M12 19l-7-7 7-7"></path>
                </svg>
                返回首页
            </button>
            
            <div class="actions">
                <button id="clear-btn" class="gallery-btn">清空</button>
                <button id="sample-btn" class="gallery-btn">加载示例</button>
            </div>
        </div>
        
        <div class="preview-main">
            <div class="code-section">
                <div class="section-title">SVG代码输入</div>
                <textarea id="svg-code" placeholder="在此输入SVG代码，可以包含一个或多个SVG标签..."></textarea>
            </div>
            
            <div class="preview-section">
                <div class="section-title">预览效果</div>
                <div class="preview-area" id="preview-area">
                    <div id="preview-placeholder" class="placeholder">
                        输入SVG代码后将在此显示预览效果
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // 获取DOM元素
        const svgCodeInput = document.getElementById('svg-code');
        const previewArea = document.getElementById('preview-area');
        const previewPlaceholder = document.getElementById('preview-placeholder');
        const clearBtn = document.getElementById('clear-btn');
        const sampleBtn = document.getElementById('sample-btn');
        
        // 示例SVG代码
        const sampleSvgCode = `<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" fill="#ff9900"/>
  <text x="50" y="55" font-size="16" text-anchor="middle" fill="white">示例</text>
</svg>

<!-- 可以包含多个SVG标签和其他内容 -->
<p>这是另一个SVG图形:</p>

<svg width="200" height="100" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="180" height="80" rx="10" fill="#3498db"/>
  <path d="M40,70 L100,30 L160,70" fill="none" stroke="#ffffff" stroke-width="4"/>
</svg>`;
        
        // 初始化事件监听器
        function init() {
            // 监听输入变化，实时更新预览
            svgCodeInput.addEventListener('input', updatePreview);
            
            // 清空按钮事件
            clearBtn.addEventListener('click', () => {
                svgCodeInput.value = '';
                updatePreview();
            });
            
            // 加载示例按钮事件
            sampleBtn.addEventListener('click', () => {
                svgCodeInput.value = sampleSvgCode;
                updatePreview();
            });
        }
        
        // 更新预览区域
        function updatePreview() {
            const svgContent = svgCodeInput.value.trim();
            
            // 完全清空预览区域
            previewArea.innerHTML = '';
            
            if (svgContent === '') {
                // 如果没有输入内容，显示占位符
                previewArea.appendChild(previewPlaceholder);
                previewPlaceholder.style.display = 'block';
                return;
            }
            
            try {
                // 创建临时容器来处理SVG内容
                const tempContainer = document.createElement('div');
                tempContainer.innerHTML = svgContent;
                
                // 创建预览容器
                const previewContainer = document.createElement('div');
                previewContainer.className = 'svg-preview-content';
                
                // 处理SVG标签和其他内容
                Array.from(tempContainer.children).forEach(child => {
                    previewContainer.appendChild(child.cloneNode(true));
                });
                
                // 完全替换预览区域内容
                previewArea.appendChild(previewContainer);
                
            } catch (error) {
                // 显示错误信息
                const errorElement = document.createElement('div');
                errorElement.className = 'error-message';
                errorElement.textContent = '解析SVG代码时出错: ' + error.message;
                errorElement.style.color = 'red';
                errorElement.style.padding = '10px';
                errorElement.style.border = '1px solid red';
                errorElement.style.borderRadius = '4px';
                
                previewArea.appendChild(errorElement);
            }
        }
        
        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', init);
    </script>
</body>
</html>